//
//  SmallViewController.m
//  A段项目
//
//  Created by cmcmhjr123 on 15/7/14.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SmallViewController.h"
#import "SANetWorkingTask.h"
#import "BaseHeader.h"
#import "SellRankTableViewCell.h"
#import "sellrank.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SellRankDetailViewController.h"


@interface SmallViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tab;
@property (nonatomic, retain) NSMutableArray *tabArr;


@end

@implementation SmallViewController
- (void)dealloc
{
    [super dealloc];
}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.tabArr = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeidth) style:UITableViewStylePlain];
    
    _tab.dataSource = self;
    _tab.delegate = self;
    _tab.rowHeight = 80;
    
    [_tab registerClass:[SellRankTableViewCell class] forCellReuseIdentifier:@"a"];
    [self.view addSubview:_tab];
    
    [SANetWorkingTask request:@"http://api.ycapp.yiche.com/indexdata/GetSerialSaleDataList/?city=1708&carlevel=1&month=2015-05&page=1&length=20"
                     parmater:nil block:^(NSDictionary *result) {
                         NSDictionary *bigdic = [result objectForKey:@"data"];
                         NSArray *arr = [bigdic objectForKey:@"list"];
                         
                         for (NSDictionary *dic in arr) {
                             sellrank *rankModel = [[sellrank alloc] init];
                             [rankModel setValuesForKeysWithDictionary:dic];
                             [_tabArr addObject:rankModel];
                             
                         }
                         [self.tab reloadData];
                         
                     }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.tabArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SellRankTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"a"];
    sellrank *model = [self.tabArr objectAtIndex:indexPath.row];
    
    if ([model.WhiteCoverImg containsString:@"{0}"]) {
        NSRange range = [model.WhiteCoverImg rangeOfString:@"{0}"];
        NSMutableString *str = [model.WhiteCoverImg mutableCopy];
        [str replaceCharactersInRange:range withString:@"3"];
        model.WhiteCoverImg = str;
    }
    
    cell.rank.text = [NSString stringWithFormat:@"%ld", model.Rank];
    cell.rank.textColor = [UIColor whiteColor];
    if (indexPath.row < 3) {
        [cell.rank setBackgroundColor:[UIColor colorWithRed:1 green:0.3 blue:0.25 alpha:1]];
    } else {
        [cell.rank setBackgroundColor:[UIColor colorWithRed:0.3 green:0.7 blue:1 alpha:1]];
    }
    
    cell.carname.text = model.SerialName;
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.WhiteCoverImg] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    NSString *min = [NSString stringWithFormat:@"%.2f~%.2f万", model.MinPrice,model.MaxPrice];
    cell.minlabel.text = min;
    
    NSString *totle = [NSString stringWithFormat:@"全国销量:%ld辆",model.Index];
    cell.totle.text = totle;
    
    
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SellRankDetailViewController *rank = [[SellRankDetailViewController alloc] init];
    sellrank *model = [self.tabArr objectAtIndex:indexPath.row];
    rank.website = model.SerialId;
    rank.name = model.SerialName;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rank];
    
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
